$(function(){
    $("#fileFiler").change(function(){
        //只上传一个文件的情况
        var fileDom = $("#fileFiler");
        var fileNum = fileDom[0].files.length;
        if(fileNum > 0){
            var fileName = fileDom[0].files[0].name;
            var fileSize = getFileSize(fileDom[0].files[0].size);
            var fileType = fileDom[0].files[0].type;
            fileType = fileName.slice(fileName.lastIndexOf(".")+1).toLowerCase();
            if(fileType != "zip"){
                layer.msg("只能选择zip格式的文件！",{time:1200});
            }else{
                $(".file-select").show();
                $("#fname").html(fileName);
                $("#fsize").html(fileSize);
                $("#ftype").html(fileType);
            }
        }
    });

    $("#uploadBtn").click(function(){
        var formData = new FormData($("#upLoadForm")[0]);
        $.ajax({
            url:"/procDef/deploy/" + getParams().id,
            type:"post",
            data:formData,
            cache:false,
            processData:false,
            contentType:false,
            success:function(res){
                if(res.success == true){
                    layer.msg('部署成功',{
                        time:1000,
                        end:function(){
                            window.parent.location.reload(); //刷新父页面
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            parent.layer.close(index); //再执行关闭
                        }
                    })
                }else{
                    layer.msg("上传失败！",{time:1000});
                }
            }
        });
    });
});


//根据文件字节数，换算单位
function getFileSize(fileByte) {
    var fileSizeByte = fileByte;
    var fileSizeMsg = "";
    if (fileSizeByte < 1048576) fileSizeMsg = (fileSizeByte / 1024).toFixed(2) + "KB";
    else if (fileSizeByte == 1048576) fileSizeMsg = "1MB";
    else if (fileSizeByte > 1048576 && fileSizeByte < 1073741824) fileSizeMsg = (fileSizeByte / (1024 * 1024)).toFixed(2) + "MB";
    else if (fileSizeByte > 1048576 && fileSizeByte == 1073741824) fileSizeMsg = "1GB";
    else if (fileSizeByte > 1073741824 && fileSizeByte < 1099511627776) fileSizeMsg = (fileSizeByte / (1024 * 1024 * 1024)).toFixed(2) + "GB";
    else fileSizeMsg = "文件超过1TB";
    return fileSizeMsg;
}

